/*
 * This file is part of JFlowMap.
 *
 * Copyright 2009 Ilya Boyandin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fons.navigator.views;


import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.WindowListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.AbstractAction;
import javax.swing.JInternalFrame;
import javax.swing.KeyStroke;

import edu.umd.cs.piccolo.PCamera;
import edu.umd.cs.piccolo.PCanvas;
import edu.umd.cs.piccolo.util.PBounds;
import fons.navigator.util.piccolo.PBoxLayoutNode;
import fons.navigator.util.piccolo.PHelp;
import fons.navigator.util.piccolo.PNodes;
import fons.navigator.util.piccolo.PanHandler;
import fons.navigator.util.piccolo.PiccoloUtils;
import fons.navigator.util.piccolo.ZoomHandler;


/**
 * @author Ilya Boyandin
 */
public class VisualCanvas extends PCanvas  {
	private static final long serialVersionUID = 1L;
	private static final String ACTION_PDF_EXPORT = "pdf-export";

	private static final Dimension MIN_SIZE = new Dimension(150, 100);
	private ZoomHandler zoomHandler;
	private boolean autoFitOnBoundsChange = true;
	private PBoxLayoutNode settingButtonsPanel;
	private PBoxLayoutNode modeButtonsPanel;
	private final PHelp helpText;

	private final View view;
	private JInternalFrame internalFrame;

	@SuppressWarnings("serial")
	public VisualCanvas(View view) {
		this.view = view;
		this.helpText = new PHelp();

		setZoomEventHandler(null);
		setZoomHandler(createZoomHandler());
		setPanEventHandler(new PanHandler());
		getInputMap().put(KeyStroke.getKeyStroke("alt P"), ACTION_PDF_EXPORT);
		getActionMap().put(ACTION_PDF_EXPORT, new AbstractAction () {

			@Override
			public void actionPerformed(ActionEvent e) {
				//tryToPaintToPdf();
			}
		});
		
		settingButtonsPanel = new PBoxLayoutNode(PBoxLayoutNode.Axis.X, 5);
		modeButtonsPanel = new PBoxLayoutNode(PBoxLayoutNode.Axis.X, 5);

		final PCamera ccam = getCamera();
		ccam.addPropertyChangeListener(PCamera.PROPERTY_BOUNDS, new PropertyChangeListener() {
			@Override
			public void propertyChange(PropertyChangeEvent evt) {
				updateButtonsPanel();
			}
		});
		ccam.addChild(modeButtonsPanel);
		ccam.addChild(settingButtonsPanel);
		ccam.addChild(helpText);		
	}
	
	public void updateButtonsPanel() {
		final PBounds b = getCamera().getBoundsReference();
		PNodes.setPosition(settingButtonsPanel, b.getMaxX() - settingButtonsPanel.getFullBoundsReference().width - 4, 4);
		PNodes.setPosition(modeButtonsPanel, b.getMaxX() - modeButtonsPanel.getFullBoundsReference().width - 4, settingButtonsPanel.getFullBoundsReference().height + 8);
	}

	public View getView() {
		return view;
	}

	public PBoxLayoutNode getSettingButtonsPanel() {
		return settingButtonsPanel;
	}

	public PBoxLayoutNode getModeButtonsPanel() {
		return modeButtonsPanel;
	}

	public ZoomHandler getZoomHandler() {
		return zoomHandler;
	}

	public void setZoomHandler(ZoomHandler handler) {
		if (zoomHandler != null) {
			removeInputEventListener(zoomHandler);
		}
		zoomHandler = handler;
		addInputEventListener(zoomHandler);
	}

	protected ZoomHandler createZoomHandler() {
		return new ZoomHandler();
	}

	public boolean getAutoFitOnBoundsChange() {
		return autoFitOnBoundsChange;
	}

	public void setAutoFitOnBoundsChange(boolean autoFitOnBoundsChange) {
		this.autoFitOnBoundsChange = autoFitOnBoundsChange;
	}

	public void setViewZoomPoint(Point2D point) {
		zoomHandler.setFixedViewZoomPoint(point);
	}

	public void setMinZoomScale(double minScale) {
		zoomHandler.setMinScale(minScale);
	}

	public void setMaxZoomScale(double maxScale) {
		zoomHandler.setMaxScale(maxScale);
	}

	public void fitChildrenInCameraView() {
		PCamera camera = getCamera();
		PBounds boundRect = getLayer().getFullBounds();
		camera.globalToLocal(boundRect);
		camera.animateViewToCenterBounds(boundRect, true, 0);
	}

	@Override
	public Dimension getMinimumSize() {
		return MIN_SIZE;
	}

	@Override
	public void setBounds(int x, int y, int w, int h) {
		if (autoFitOnBoundsChange) {
			if (x != getX()  ||  y != getY()  ||  w != getWidth()  ||  h != getHeight()) {
				Rectangle2D oldVisibleBounds =  getVisibleBounds();
				Rectangle2D oldContentBounds = getViewContentBounds();

				super.setBounds(x, y, w, h);

				if (oldVisibleBounds != null) {
					fitVisibleInCameraView(oldVisibleBounds, oldContentBounds);
				}
			}
		} else {
			super.setBounds(x, y, w, h);
		}
	}

	private static final Insets NULL_INSETS = new Insets(0, 0, 0, 0);

	public void fitVisibleInCameraView(Rectangle2D visibleBounds, Rectangle2D contentBounds) {
		if (contentBounds.getWidth() <= 0 || contentBounds.getHeight() <= 0) {
			return;
		}
		final Insets insets;
		//    if (contentBounds.contains(visibleBounds)) { // the whole  is inside the view
			//      insets = getContentInsets();
		//      insets.left += 5;
		//      insets.top += 5;
		//      insets.bottom += 5;
		//      insets.right += 5;
		//    } else {
		insets = NULL_INSETS;
		//    }
		PiccoloUtils.setViewPaddedBounds(getCamera(), visibleBounds, insets);
	}


	/**
	 * Bounds of the part of the visualization (all of the Nodes together) that
	 * is currently visible in the view (in the view coordinate system).
	 *
	 * @return
	 */
	public Rectangle2D getVisibleBounds() {
		//      final PBounds mb = getBounds();
		final PBounds mb = getLayer().getFullBounds();
		if (mb != null) {
			final PBounds vb = getCamera().getViewBounds();
			Rectangle2D.intersect(vb, mb, vb);
			return vb;
		}
		return null;
	}

	/**
	 * Bounds of the area where the visualization should be placed (without the floating
	 * panels) in the camera coordinate system.
	 *
	 * @return
	 */
	private Rectangle2D getContentBounds() {
		//    final Insets insets = getContentInsets();
		//    final PBounds vb = getCamera().getBounds();
		//    vb.x += insets.left;
		//    vb.y += insets.top;
		//    vb.width -= insets.left + insets.right;
		//    vb.height -= insets.top + insets.bottom;
		//    return vb;
		return getBounds();
	}

	private Rectangle2D getViewContentBounds() {
		final Rectangle2D cb = getContentBounds();
		getCamera().localToView(cb);
		return cb;
	}

	/*public void tryToPaintToPdf (File file) {
		try {
			paintToPdf(new FileOutputStream(file));    
		} catch (Exception ex) {
			showProblemDialog(ex);
		}
	}*/

	/*public void tryToPaintToPdf() {
		File f = getOutputFilename(".pdf");
		tryToPaintToPdf(f);
	}
	
	public void showProblemDialog(Exception ex) {
		JMsgPane.showProblemDialog(this,
				ex.getClass().getSimpleName() + " " + ex.getMessage() + " " +
						(ex.getCause() != null ? ex.getCause().getMessage() : ""));
	}

	public void paintToPdf(OutputStream out) throws IOException, DocumentException {
	}

	public File getOutputFilename(String ext) {


		File file = new File("output" + ext);
		int i = 0;
		while (file.exists()) {
			i++;
			file = new File("output-" + i + ext);
		}
		return file;
	}*/

	public PHelp getHelpText() {
		return helpText;
	}

	public WindowListener getWindowListener() {
		return this.view.getWindowListener();
	}

	public JInternalFrame getInternalFrame() {
		return internalFrame;
	}

	public void setInternalFrame(JInternalFrame internalFrame) {
		this.internalFrame = internalFrame;
	}
}


